/**
 * A useful FTP client tools which used enterprisedt.com ftp componet
 */
package com.sasis.webapp.task.ftp;


import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
//import org.apache.log4j.Logger;

import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPFile;
//import com.enterprisedt.net.ftp.FTPConnectMode;
//import com.enterprisedt.net.ftp.FTPTransferType;
/**
 * // TODO repeat count
 * @author Donitz
 * @since 2007-9-13
 */
public class FtpClientBKOImpl extends AbstractFtpClient implements IFtpClient{
	private static Log log = LogFactory.getLog(FtpClientBKOImpl.class);
	
	private FTPClient ftpClient = new FTPClient();
	private String remoteTransTagFileName;
//	private FTPConnectMode fTPConnectMode;
//	private FTPTransferType fTPTransferType;
	/**
	 * 
	 */
	public FtpClientBKOImpl() {
		
	}
	
	public boolean download(String localPath,String remoteFile){
		try {
			ftpClient.get(localPath, remoteFile);
			return true;
		} catch (IOException e) {
			log.error("ERROR FTP download IO ", e);
		} catch (FTPException e) {
			log.error("ERROR FTP download FTP ", e);
		}
		return false;
	}
	
	public void upload(){
		
	}
	
	public void closeConn(){
		try {
			if(ftpClient==null){
				log.info("FTPClient is null, not need for close");
				return;
			}
			if(ftpClient.connected()){
				ftpClient.quit();
			}
		} catch (IOException e) {
			log.error("ERROR FTP close IO ", e);
		} catch (FTPException e) {
			log.error("ERROR FTP close FTP ", e);
		}
	}
	
	public boolean openConn(){
		try {
			if(timeout>0){
				ftpClient.setTimeout(timeout);
			}
			ftpClient.setRemoteHost(hostAddress);
			ftpClient.setRemotePort(port);
//			ftpClient.setConnectMode(FTPConnectMode.PASV); 
//			ftpClient.setType(FTPTransferType.ASCII);
			if(StringUtils.isNotBlank(this.socksAddress)&&this.socksPort>0){
				FTPClient.initSOCKS(this.socksPort+"",this.socksAddress);
			}
			ftpClient.connect();
			ftpClient.login(user, password);	
		} catch (IOException e) {
			log.error("ERROR FTP conn IO ", e);
			return false;
		} catch (FTPException e) {
			log.error("ERROR FTP conn FTP ", e);
			return false;
		}
		return true;
	}

	public String[] ls(String remotePath){
		try {
		if(StringUtils.isNotBlank(remotePath)){
			return ftpClient.dir(remotePath);
		}
		    return ftpClient.dir();
		} catch (IOException e) {
			log.error("ERROR FTP ls IO ", e);
		} catch (FTPException e) {
			log.error("ERROR FTP ls FTP ", e);
		}
		return null;
	}
	
	/* (non-Javadoc)
	 * @see com.sasis.webapp.task.ftp.IFtpClient#lsDetail(java.lang.String)
	 * TODO not complete
	 */
	public FtpContent[] lsDetail(String remotePath) {
		FTPFile[] ftpFileArrays = null;
		boolean foundTag = false;
		try {
			ftpFileArrays = ftpClient.dirDetails(remotePath);
		} catch (IOException e) {
			log.error("ERROR FTP IO",e);
			return null;
		} catch (FTPException e) {
			log.error("ERROR FTP ",e);
			return null;
		} catch (ParseException e) {
			log.error("ERROR FTP Parse",e);
			return null;
		}
		if(ftpFileArrays==null&&ftpFileArrays.length==0){
			return null;
		}
		FtpContent[] ftpContentArrays = new FtpContent[ftpFileArrays.length];		
		for(int i=0;i<ftpFileArrays.length;i++){			
			String remoteFileName = ftpFileArrays[i].getName();
			Date createTime = ftpFileArrays[i].created();
			if(!foundTag&&StringUtils.equalsIgnoreCase(remoteFileName, this.remoteTransTagFileName)){
				ftpContentArrays[0].setFileName(remoteFileName);
				ftpContentArrays[0].setCreateTime(createTime);
				foundTag = true;
			}else if(i==ftpFileArrays.length){
				log.debug("ERROR : Couldn't find '"+this.remoteTransTagFileName+"' which is a tag trans file!!! ");
				return null;
			}else{
				ftpContentArrays[i+1].setFileName(remoteFileName);
				ftpContentArrays[i+1].setCreateTime(createTime);
			}
		}
		return ftpContentArrays;
	}
	
	public boolean mv(String fromFile,String toPath){
		try {
			ftpClient.rename(fromFile, toPath+fromFile);
		} catch (IOException e) {
			log.error("ERROR FTP mv IO ", e);
			return false;
		} catch (FTPException e) {
			log.error("ERROR FTP mv FTP ", e);
			return false;
		}
		return true;
	}
	

	public FTPClient getFtpClient() {
		return ftpClient;
	}

	public void setFtpClient(FTPClient ftpClient) {
		this.ftpClient = ftpClient;
	}

	public boolean isConnected() {
		return this.ftpClient.connected();
	}


//	public void hostAdress(String hostName, int port) {
//		this.hostAddress=hostName;
//		this.port=port;
//		
//	}
//
//	public void login(String loginName, String password) {
//		this.user=loginName;
//		this.password=password;
//		
//	}
//
//	public void socksAddress(String socksAdd, int socksPort) {
//		this.socksAddress=socksAdd;
//		this.socksPort = socksPort;
//	}

	

	
}
